package framework.view.components.button
{
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.text.TextField;

import framework.proxy.ComponentProxy;

/**
 * StateButtonProxy
 * @author jliang
 * 
 */
public class StateButtonProxy extends ComponentProxy
{
	//==========================================================================
	//  Class constants
	//==========================================================================
	private static const OUT_STATE:String = "out";
	private static const OVER_STATE:String = "over";
	private static const SELECTED_STATE:String = "selected";
	private static const DISABLE_STATE:String = "disable";
	//==========================================================================
	//  Constructor
	//==========================================================================
	public function StateButtonProxy()
	{
		super();
	}
	//==========================================================================
	//  Properties
	//==========================================================================
	protected var label:TextField;
	
	protected var _selected:Boolean = false;

	public function get selected():Boolean
	{
		return _selected;
	}

	public function set selected(value:Boolean):void
	{
		if (value && !_selected)
		{
			mouseUpHandler(null);
			return;
		}
		else if (!value && _selected)
		{
			skin.gotoAndStop(OUT_STATE);
		}
		_selected = value;
	}
	
	protected var _onSelected:Function;
	
	public function get onSelected():Function
	{
		return _onSelected;
	}
	
	public function set onSelected(value:Function):void
	{
		_onSelected = value;
	}
	
	protected var _onMouseOver:Function;
	
	public function get onMouseOver():Function
	{
		return _onMouseOver;
	}
	
	public function set onMouseOver(value:Function):void
	{
		_onMouseOver = value;
	}
	
	protected var _onMouseOut:Function;
	
	public function get onMouseOut():Function
	{
		return _onMouseOut;
	}
	
	public function set onMouseOut(value:Function):void
	{
		_onMouseOut = value;
	}
	
	//==========================================================================
	//  Overridden methods: ComponentProxy
	//==========================================================================
	override public function parse(target:MovieClip):void
	{
		super.parse(target);
		
		label = skin.getChildByName("label") as TextField;
		skin.gotoAndStop(OUT_STATE);
		
		skin.mouseChildren = false;
		skin.buttonMode = true;
		
		skin.addEventListener(MouseEvent.MOUSE_OVER, mouseOverHandler);
		skin.addEventListener(MouseEvent.MOUSE_OUT, mouseOutHandler);
		skin.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
		skin.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
	}
	
	override public function dispose():void
	{
		if (skin)
		{            
			skin.removeEventListener(MouseEvent.MOUSE_OVER, mouseOverHandler);
			skin.removeEventListener(MouseEvent.MOUSE_OUT, mouseOutHandler);
			skin.removeEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
			skin.removeEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
		}
		onMouseOver = null;
		onMouseOut = null;
		onSelected = null;
	}
	//==========================================================================
	//  Public methods
	//==========================================================================
	public function set text(value:String):void
	{
		label.text = value;
	}
	
	public function get text():String
	{
		return label.text;
	}
	
	public function set embedFont(value:Boolean):void
	{
		label.embedFonts = value;
	}
	
	public function get embedFont():Boolean
	{
		return label.embedFonts;
	}
	
	protected function mouseOverHandler(event:Event):void
	{
		if (!selected) 
		{
			skin.gotoAndStop(OVER_STATE);
		}
		if (onMouseOver != null)
		{
			onMouseOver();
		}
	}
	
	protected function mouseOutHandler(event:Event):void
	{
		if (!selected) 
		{
			skin.gotoAndStop(OUT_STATE);
		}
		if (onMouseOut != null)
		{
			onMouseOut();
		}
	}
	
	protected function mouseDownHandler(event:Event):void
	{
		
	}
	
	protected function mouseUpHandler(event:Event):void
	{
		skin.gotoAndStop(SELECTED_STATE);
		_selected = true;
		if (onSelected != null)
		{
			onSelected();
		}
	}
	
}
}